package com.cskaoyan.mall.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.orders.OrdersByPageBO;
import com.cskaoyan.mall.model.orders.OrdersByPageVO;
import com.cskaoyan.mall.model.orders.OrderByIdVO;
import com.cskaoyan.mall.model.orders.changeOrderBO;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.OrderServiceImpl;
import com.cskaoyan.mall.util.RequestBodyUtils;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("ordersByPage".equals(op)) {
            orderByPage(request, response);
        }else if("changeOrder".equals(op)){
            changeOrder(request,response);
        }
    }

    private void orderByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        OrdersByPageBO ordersByPageBO = gson.fromJson(requestBody, OrdersByPageBO.class);
        // 金额上下限一旦输入必须是数字
        try {
            if (!StringUtils.isEmpty(ordersByPageBO.getMoneyLimit1()))
                Double.parseDouble(ordersByPageBO.getMoneyLimit1());
            if (!StringUtils.isEmpty(ordersByPageBO.getMoneyLimit2()))
                Double.parseDouble(ordersByPageBO.getMoneyLimit2());
        } catch (Exception e) {
            Result result = new Result(10000, "金额上下限不为数字", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        OrdersByPageVO ordersByPageVO = orderService.orderByPage(ordersByPageBO);
        Result result = new Result(0,null,ordersByPageVO);
        response.getWriter().println(gson.toJson(result));
    }

    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        changeOrderBO changeOrderBO = gson.fromJson(requestBody, changeOrderBO.class);
        boolean status = orderService.changeOrder(changeOrderBO);
        Result result;
        if (status) {
            result = new Result(0, "修改成功", null);
        } else {
            result = new Result(10000, "修改失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("deleteOrder".equals(op)) {
            deleteOrder(request, response);
        }else if ("order".equals(op)) {
            getOrderById(request, response);
        }
    }

    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean status = orderService.deleteOrder(id);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }


    private void getOrderById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        OrderByIdVO orderByIdVo = orderService.getOrderById(id);
        Result result = new Result(0,null, orderByIdVo);
        response.getWriter().println(gson.toJson(result));
    }
}
